package com.wyzz.hr.action;

import java.math.BigDecimal;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.wf.leaverequest.LrWfApproverAction;
import com.wyzz.hr.bo.ILeavebalanceBO;
import com.wyzz.hr.bo.ILeaverequestBO;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.bo.impl.LrDataCheckImpl;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leavebalance;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.vo.ExaminSearchBean;
import com.wyzz.hr.vo.LeaveTotalBean;

public class MyLeaveSearchAction extends LrWfApproverAction {

	public MyLeaveSearchAction() {
	}

	public String execute() throws Exception {
		srcAction = "myLeaveSearch";
		initLRLists();
		if (page == null) {
			page = new Pager();
			page.setOrder("");
		}
		if (es_Bean == null)
			es_Bean = new ExaminSearchBean();
		DetachedCriteria dc = DetachedCriteria.forClass(Leaverequest.class);
		BaseCrit.addDC(dc, (new StringBuilder()).append(
				Leaverequest.PROP_LR_EMP_NO).append(".").append(
				Employee.PROP_ID).toString(), "eq",
				new String[] { getCurrentEmpNo() });
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		lr_BO.examinSearch(es_Bean, dc, page);
		Calendar cal = Calendar.getInstance();
		currEmpName = getCurrentEmp().getEmpName();
		currYear = String.valueOf(cal.get(1));
		if (getRequest().getAttribute("msg") != null)
			addSuccessInfo((String) getRequest().getAttribute("msg"));
		if (!StringUtils.isEmpty(infoMeg))
			addSuccessInfo(infoMeg);
		if (!StringUtils.isEmpty(errorMsg))
			addErrorInfo(errorMsg);
		return "success";
	}

	public Map getLeaveTotal(String empId, int dayOrHour, String actionURL) {
		Map result = new HashMap();
		if (!StringUtils.isEmpty(actionURL)
				&& !"HR".equals(DWRUtil.checkAuth(actionURL, "execute"))) {
			result.put("error",
					"\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01");
			return result;
		}
		if (StringUtils.isEmpty(empId))
			empId = getCurrentEmpNo();
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		Map map = lr_BO.getLeaveTotal(empId);
		lrTypeList = getDrillDown("LeaveType", new String[0]);
		ILeavebalanceBO lb_BO = (ILeavebalanceBO) getBean("leavebalanceBO");
		LrDataCheckImpl lrAddCheckData = (LrDataCheckImpl) getBean("lrDataCheck");
		double hoursPerDay = ExaminDateUtil.getDefaultHoursPerDay();
		Employee currEmp = new Employee(empId);
		Calendar c = Calendar.getInstance();
		String thisYear = String.valueOf(c.get(1));
		List totalBeanList = new ArrayList();
		Iterator i$;
		LeaveTotalBean bean;
		for (i$ = lrTypeList.iterator(); i$.hasNext(); totalBeanList.add(bean)) {
			Leavetype lt = (Leavetype) i$.next();
			bean = (LeaveTotalBean) map.get(lt.getLtName());
			if (bean == null) {
				bean = new LeaveTotalBean();
				bean.setType(lt.getLtName());
				bean.setToApproveDays("0.0");
				bean.setTotaldays("0.0");
			}
			bean.setId(lt.getLtNo());
			Leavebalance leaveBalance = lb_BO.getLeavebalance(currEmp,
					thisYear, lt);
			double leaveDate[] = { 0.0D, 0.0D };
			switch (lt.getLtSpecialCat().intValue()) {
			case 1: // '\001'
				if (leaveBalance == null) {
					bean.setCanUseTotals("......");
					bean.setUseTotals("......");
					break;
				}
				leaveDate = lrAddCheckData.getAnnualLeaveDate(currEmp, lt,
						leaveBalance);
				bean.setCanUseTotals(String.valueOf(MyTools.round(leaveDate[1]
						/ hoursPerDay, 2)));
				if (leaveBalance.getLbDaysOfYear() == null)
					bean.setUseTotals("0.0");
				else
					bean.setUseTotals(String.valueOf(leaveBalance
							.getLbDaysOfYear().doubleValue()));
				break;

			case 5: // '\005'
				if (leaveBalance == null) {
					bean.setCanUseTotals("......");
					bean.setUseTotals("......");
					break;
				}
				leaveDate = lrAddCheckData.getSick02LeaveDate(currEmp, lt,
						leaveBalance);
				bean.setCanUseTotals(String.valueOf(MyTools.round(leaveDate[1]
						/ hoursPerDay, 2)));
				if (leaveBalance.getLbDaysOfYear() == null)
					bean.setUseTotals("0.0");
				else
					bean.setUseTotals(String.valueOf(leaveBalance
							.getLbDaysOfYear().doubleValue()));
				break;

			default:
				bean.setCanUseTotals("......");
				bean.setUseTotals("......");
				break;
			}
		}

		result.put("param_succ", totalBeanList);
		if (dayOrHour == 0)
			return result;
		i$ = totalBeanList.iterator();
		do {
			if (!i$.hasNext())
				break;
			LeaveTotalBean ltb = (LeaveTotalBean) i$.next();
			ltb.setTotaldays(ltb.getTotalhours());
			ltb.setToApproveDays(ltb.getToApproveHours());
			if (!StringUtils.isEmpty(ltb.getCanUseTotals())
					&& !"......".equals(ltb.getCanUseTotals()))
				ltb.setCanUseTotals(String.valueOf(Double.parseDouble(ltb
						.getCanUseTotals())
						* hoursPerDay));
			if (!StringUtils.isEmpty(ltb.getUseTotals())
					&& !"......".equals(ltb.getCanUseTotals()))
				ltb.setUseTotals(String.valueOf(Double.parseDouble(ltb
						.getUseTotals())
						* hoursPerDay));
		} while (true);
		return result;
	}

	public void validate() {
		validateDate(es_Bean);
		validateLRStatus(es_Bean);
	}

	public String delete(String lrId, HttpSession session) {
		ILeaverequestBO lr_BO = (ILeaverequestBO) getBean("leaverequestBO");
		Leaverequest lr = lr_BO.loadOneLeaverequest(lrId, null);
		if (lr == null || lr.getLrStatus().intValue() != 21
				|| !getCurrentEmp().equals(lr.getLrEmpNo())
				&& !getCurrentEmp().equals(lr.getLrCreateBy()))
			return "\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF01";
		List result = lr_BO.delLeaveRequest(lrId, getCurrentEmp());
		if (result.size() > 0)
			return (String) result.get(0);
		else
			return "suc";
	}

	public ExaminSearchBean getEs_Bean() {
		return es_Bean;
	}

	public void setEs_Bean(ExaminSearchBean es_Bean) {
		this.es_Bean = es_Bean;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getSrcAction() {
		return srcAction;
	}

	public void setSrcAction(String srcAction) {
		this.srcAction = srcAction;
	}

	public String getCurrEmpName() {
		return currEmpName;
	}

	public void setCurrEmpName(String currEmpName) {
		this.currEmpName = currEmpName;
	}

	public String getCurrYear() {
		return currYear;
	}

	public void setCurrYear(String currYear) {
		this.currYear = currYear;
	}

	private static final long serialVersionUID = 1L;
	private ExaminSearchBean es_Bean;
	private Pager page;
	private String srcAction;
	private String currEmpName;
	private String currYear;
}
